<?php
/**
 * System_Model_DbTable_Employee
 *
 * @author maomao.thx@gmail.com
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: Employee.php 906 2011-10-20 02:38:33Z mmxie $
 */
class System_Model_DbTable_Employee extends IG_Model_DbTable
{

    protected $_name = 'sys_employee';

    protected $_primary = 'employee_id';

    protected $_dependentTables = array(
        'System_Model_DbTable_UserMenu',
        'System_Model_DbTable_EmployeeRole'
    );

    public function loginError($name)
    {
        $sql = "UPDATE `$this->_name` SET "
             . "`error_times`=`error_times`+1, "
             . "`last_error`=? "
             . "WHERE `name`=?";
        return $this->getAdapter()->query($sql, array(date('Y-m-d H:i:s'), $name));
    }

    public function getRoleByEmployee($employeeId)
    {
        $this->createRow(array('employee_id' => $employeeId))
             ->findManyToManyRowset('System_Model_DbTable_Role',
                                    'System_Model_DbTable_EmployeeRole');
             exit('d');
        return $this->select()->setIntegrityCheck(false)
                    ->from($this->_name, array('role_id'))
                    ->join('sys_role', "$this->_name.role_id = sys_role.role_id", array('name'))
                    ->where("$this->_name.employee_id=?", $employeeId)
                    ->query(PDO::FETCH_KEY_PAIR)->fetchAll()
                    ;
    }

    public function getRoles($id)
    {
        return $this->createRow(array('employee_id' => $id))
                    ->findManyToManyRowset('System_Model_DbTable_Role',
                                           'System_Model_DbTable_EmployeeRole');
    }
}

